package com.lebang.controller.api.earned.book;

import com.lebang.common.MemberLock;
import com.lebang.common.Require;
import com.lebang.component.RedisClient;
import com.lebang.entity.RequestHeaders;
import com.lebang.feign.BookTimeFeign;
import com.lebang.model.RequestUserInfo;
import com.lebang.model.back.Result;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.service.MemberNoticeService;
import com.lebang.service.PointService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 听书时长
 *
 * @author ljx
 * @date 2020-11-02
 */
@Api(tags = "听书时长")
@RestController
@RequestMapping("/book/time")
public class BookTimeApi {
    @Autowired
    private BookTimeFeign bookTimeFeign;
    @Autowired
    private PointService pointService;
    @Autowired
    private MemberNoticeService memberNoticeService;


    /**
     * 获取听书时长阶段配置信息
     *
     * @param request
     * @return
     */
    @Require
    @ApiOperation("获取听书时长阶段配置信息")
    @GetMapping("/configList")
    public JsonData configList(HttpServletRequest request) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        return bookTimeFeign.configList(userInfo);
    }

    /**
     * 听书时长配置记录添加
     *
     * @param request
     * @param configId
     * @return
     */
    @Require
    @ApiOperation("听书时长配置记录添加")
    @PostMapping("/timeConfig/addDataRecord")
    @MemberLock
    public JsonData bookTimeData(HttpServletRequest request, @ApiParam(name = "配置id") @RequestParam("configId") Integer configId) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        Result<Integer> result = bookTimeFeign.bookTimeData(userInfo, configId);
        if (result.isSuccess()) {
            return Callback.success(result.getStatus(), result.getMessage(), result.getData());
        } else {
            return Callback.fail(result.getStatus(), result.getMessage());
        }
    }

    /**
     * 听书时长领奖
     *
     * @param request
     * @param recordId
     * @return
     */
    @Require
    @ApiOperation("听书时长领奖")
    @PostMapping("/time/pull")
    @MemberLock
    public JsonData bookTimePull(HttpServletRequest request, @ApiParam(name = "记录id") @RequestParam("recordId") Integer recordId) {
        if (recordId == null) {
            return Callback.fail("fail", "领取失败");
        }
        RequestUserInfo userInfo = new RequestUserInfo(request);
        RequestHeaders headers = new RequestHeaders(request);

        Result<Integer> result = bookTimeFeign.bookTimePull(userInfo, recordId);
        if (result.isSuccess()) {
            pointService.addPoint(userInfo.getMemberId(), result.getData(),
                    TaskCategory.BOOK_TIME, headers, "完成听书时长阶段奖励" + result.getData() + "金币");
            memberNoticeService.addNoticeAndPush("听书赚", "完成听书时长阶段奖励" + result.getData() + "金币", headers.getUserid(), headers);
            return Callback.success(result.getStatus(), result.getMessage(), result.getData());
        } else {
            return Callback.fail(result.getStatus(), result.getMessage());
        }
    }

    /**
     * 听书时长记录添加
     *
     * @param request
     * @param time
     * @return
     */
    @Require
    @ApiOperation("听书时长记录添加")
    @PostMapping("/add/bookTime")
    @MemberLock
    public JsonData addTimeRecord(HttpServletRequest request,
                                  @ApiParam(name = "时长") @RequestParam("time") Long time,
                                  @ApiParam(name = "听书id") @RequestParam("time") String bookId) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        return bookTimeFeign.addTimeRecord(userInfo,time,bookId);
    }

    /**
     * 获取今日听书时长
     *
     * @param request
     * @return
     */
    @Require
    @ApiOperation("获取今日听书时长")
    @GetMapping("/get/bookTime")
    public JsonData getBookTime(HttpServletRequest request) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        return bookTimeFeign.getBookTime(userInfo);
    }

}
